<!DOCTYPE html>
<html class="x-admin-sm">

<head>
  {include file="public/head"}
</head>

<body>
  <div class="x-body">
    <form class="layui-form">
      <div class="layui-form-item">
        <input type="hidden" name="id" value="{$data.item.id}">
        <label class="layui-form-label">用户名</label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" name="username" value="{$data.item.username}" {$data.item.id>0?'readonly':''}>
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">角&nbsp;&nbsp;&nbsp;&nbsp;色</label>
        <div class="layui-input-inline">
          <select name="gid">
            <option value=0></option>
            {volist name="data.groups" id="vo"}
            <option value="{$vo.gid}" {$vo.gid == $data.item.gid?'selected':''}>{$vo.title}</option>
            {/volist}
          </select>
        </div>
      </div>
      
      {if !$data.item.id}
      <div class="layui-form-item">
        <label class="layui-form-label">密&nbsp;&nbsp;&nbsp;&nbsp;码</label>
        <div class="layui-input-inline">
          <input type="password" class="layui-input" name="pwd">
        </div>
      </div>
      {/if}

      <div class="layui-form-item">
        <label class="layui-form-label">姓&nbsp;&nbsp;&nbsp;&nbsp;名</label>
        <div class="layui-input-inline">
          <input type="text" class="layui-input" name="truename" value="{$data.item.truename}">
        </div>
      </div>

      <div class="layui-form-item">
        <label class="layui-form-label">状&nbsp;&nbsp;&nbsp;&nbsp;态</label>
        <div class="layui-input-inline">
          <input type="checkbox" name="status" lay-skin="primary" title="禁用" value="1" {$data.item.status?'checked':''}>
        </div>
      </div>
    </form>
    <div class="layui-form-item">
      <div class="layui-input-block">
        <button class="layui-btn" onclick="save()">保存</button>
      </div>
    </div>
  </div>
  <script>
    layui.use(['form', 'layer'], function () {
      $ = layui.jquery,
        form = layui.form,
        layer = layui.layer;
    });
    // 保存管理员
    function save() {
      var id = parseInt($('input[name="id"]').val());
      var username = $.trim($('input[name="username"]').val());
      var pwd = $.trim($('input[name="pwd"]').val());
      var gid = $('select[name="gid"]').val();
      var truename = $.trim($('input[name="truename"]').val());
      if (username == '') {
        layer.alert('请输入用户名', { icon: 2 });
        return;
      }
      if (isNaN(id) && pwd == '') {
        layer.alert('请输入密码', { icon: 2 });
        return;
      }
      if (gid == 0) {
        layer.alert('请选择角色', { icon: 2 });
        return;
      }
      if (truename == '') {
        layer.alert('请输入姓名', { icon: 2 });
        return;
      }

      $.post('/admin.php/Admin/save', $('form').serialize(), function (res) {
        if (res.code > 0) {
          layer.alert(res.msg, { icon: 2 });
        } else {
          layer.msg(res.msg);
          setTimeout(function () { parent.window.location.reload(); }, 1000);
        }
      }, 'json');
    }
  </script>
</body>

</html>